﻿using System;
using TimeMachine.Engine.Vcs;

namespace TimeMachine.Engine.Reports
{
	public class TimeRange
	{
		private readonly DateTime since;
		private readonly int sinceRev;
		private readonly DateTime till;
		private readonly int tillRev;

		public TimeRange(DateTime since, DateTime till)
		{
			this.since = since;
			this.till = till;
			sinceRev = -1;
			tillRev = -1;
		}

		public TimeRange(int sinceRev, int tillRev)
		{
			this.sinceRev = sinceRev;
			this.tillRev = tillRev;
		}

		public TimeRange(DateTime since, int tillRev)
		{
			this.since = since;
			this.tillRev = tillRev;
			sinceRev = -1;
		}

		public TimeRange(int sinceRev, DateTime till)
		{
			this.sinceRev = sinceRev;
			this.till = till;
			tillRev = -1;
		}

		public bool Containes(CommitInfo commit)
		{
			return Compare(since, sinceRev, commit) <= 0 && Compare(till, tillRev, commit) >= 0;
		}

		private static int Compare(DateTime p1, int p1Rev, CommitInfo commit)
		{
			if(p1Rev >= 0) return p1Rev.CompareTo(commit.Revision);
			return p1.CompareTo(commit.Date);
		}
	}
}